package com.turbomanage.storm.apt;

public class SqlUtil {

	private static String reservedWords = "ABORT,ACTION,ADD,AFTER,ALL,ALTER,ANALYZE,AND,AS,ASC,ATTACH,AUTOINCREMENT,BEFORE,BEGIN,BETWEEN,BY,CASCADE,CASE,CAST,CHECK,COLLATE,COLUMN,COMMIT,CONFLICT,CONSTRAINT,CREATE,CROSS,CURRENT_DATE,CURRENT_TIME,CURRENT_TIMESTAMP,DATABASE,DEFAULT,DEFERRABLE,DEFERRED,DELETE,DESC,DETACH,DISTINCT,DROP,EACH,ELSE,END,ESCAPE,EXCEPT,EXCLUSIVE,EXISTS,EXPLAIN,FAIL,FOR,FOREIGN,FROM,FULL,GLOB,GROUP,HAVING,IF,IGNORE,IMMEDIATE,IN,INDEX,INDEXED,INITIALLY,INNER,INSERT,INSTEAD,INTERSECT,INTO,IS,ISNULL,JOIN,KEY,LEFT,LIKE,LIMIT,MATCH,NATURAL,NO,NOT,NOTNULL,NULL,OF,OFFSET,ON,OR,ORDER,OUTER,PLAN,PRAGMA,PRIMARY,QUERY,RAISE,REFERENCES,REGEXP,REINDEX,RELEASE,RENAME,REPLACE,RESTRICT,RIGHT,ROLLBACK,ROW,SAVEPOINT,SELECT,SET,TABLE,TEMP,TEMPORARY,THEN,TO,TRANSACTION,TRIGGER,UNION,UNIQUE,UPDATE,USING,VACUUM,VALUES,VIEW,VIRTUAL,WHEN,WHERE";

	public static boolean isValidIdentifier(String id) {
		// valid Java identifier in brackets may include SQL reserved words
		if (id.matches("^\\[[_a-zA-z][_a-zA-Z0-9]*\\]$"))
			return true;
		// Allow only valid Java identifiers since a table name is
		// also used as the name of the corresponding TableHelper class
		if (!isValidJavaIdentifier(id))
			return false;
		if (isReservedWord(id))
			return false;
		
		return true;
	}

	private static boolean isReservedWord(String id) {
		return reservedWords.contains(id.toUpperCase());
	}

	public static boolean isValidJavaIdentifier(String id) {
		return id.matches("^[_a-zA-z][_a-zA-Z0-9]*$");
	}
	
}